﻿using System;
using System.Linq;
using System.Windows.Forms;
using Relocation.Data;
using Relocation.Com.Tools;
using Relocation.Base;
using Relocation.Com;

namespace Relocation.Forms
{
	public partial class VillagesWindow : Base.UI.BaseForm
	{

		#region 变量声明	

		/// <summary>
		/// 当前页面的实体对象
		/// </summary>
		Village Villages;
	
		#endregion


		#region 构造函数

		public VillagesWindow():base()
		{
			Initialize();
		}

		public VillagesWindow(Session session)
			: base(session)
		{
			Initialize();
		}

		public VillagesWindow(Session session, Village villages)
			: this(session)
		{
			this.Villages = villages;
		}

		private void Initialize()
		{
			InitializeComponent();
			this.TextBox_Name.Tag = new ControlTag(Village.GetPropName(t => t.name), new ValidatorInfo(new ValidatorInfo.ValidatorHandler(delegate(Control control) {
				control.Text=control.Text.Trim();
				if (string.IsNullOrEmpty(control.Text))
				{
					return new ValidatorInfo.ValidatorResult(false, "必须填写！");
				} else
				{
					bool isExists = (this.Villages == null) ?
						(this.Session.DataModel.Villages.Any(t => control.Text.Equals(t.name)))
						:
						(this.Session.DataModel.Villages.Any(t => control.Text.Equals(t.name)&&(!t.village_id.Equals(this.Villages.village_id))));
					return new ValidatorInfo.ValidatorResult(!isExists, isExists ? "单位名称重复" : string.Empty);
				}
			})));
			this.TextBox_Principal.Tag=new ControlTag(Village.GetPropName(t=>t.principal));
			this.TextBox_Phone_numbe.Tag = new ControlTag(Village.GetPropName(t => t.phone_numbe));
			this.TextBox_Place.Tag = new ControlTag(Village.GetPropName(t => t.place));
			this.TextBox_Remark.Tag = new ControlTag(Village.GetPropName(t => t.remark));
			this.ButtonSave.Tag = new ControlTag(new RoleInfo(Session.KEY_ROLE_ADMIN, Session.KEY_ROLE_OPERATOR));			
		}
		
		#endregion

		/// <summary>
		/// 保存按钮
		/// </summary>		
		private void ButtonSave_Click(object sender, EventArgs e)
		{
			try
			{
				if (!this.ValidatorAllControls())
					return;

				//新增
				if (this.Villages == null)
				{
					this.Villages = new Village();
					this.Villages.village_id = Relocation.Service.DataModel.GenerateID(this.Villages);
					this.Villages.created = System.DateTime.Now;
					this.Villages.Project = this.Session.Project;
				}

				this.GetValueFromInput(this.Villages);

				this.Villages.updated = System.DateTime.Now;
				this.Session.DataModel.Save(this.Villages);
				this.FieldIsChanged = false;
				this.DialogResult = DialogResult.OK;
				this.Close();
			} catch (Exception ex)
			{				
				Log.Error(ex.GetInnerExceptionMessage());
				System.Diagnostics.Debug.WriteLine(ex);
				MyMessagebox.Show("操作失败！");
			}
		}

		private void VillagesWindow_Load(object sender, EventArgs e)
		{
			try
			{
				if (this.Villages != null)
				{
					this.Session.DataModel.Refresh(System.Data.Objects.RefreshMode.ClientWins, this.Villages);
					this.SetValueToInput(this.Villages);
					this.FieldIsChanged = false;
				}
			} catch (Exception ex)
			{				
				Log.Error(ex.GetInnerExceptionMessage());
				System.Diagnostics.Debug.WriteLine(ex);
				MyMessagebox.Show("操作失败！");
			}
		}
		
	}
}
